#include "msg_sort_process.h"
#include "dbg_trace.h"

/****************************************************************
							变量区
*****************************************************************/
MSG_INFO_TABLE_T g_DevNetStTable;
MY_U8	*g_ArrayMsgIndex[MAX_NODE_PROC_NUM] = {NULL, };
/****************************************************************/


void DevNetStTableInit(void)
{
	MY_U8	i = 0;

	for(i = 0; i < MAX_NODE_PROC_NUM; i++)
	{
		g_DevNetStTable.NodeMsgInfo[i].u8Index = i;
		g_DevNetStTable.NodeMsgInfo[i].u8IsLoad = E_MSG_IDLE;
		g_DevNetStTable.NodeMsgInfo[i].u16CurrSeqNum = i+1;
	}
	g_DevNetStTable.u16AddSeqNum = i;
	g_DevNetStTable.u8NodesSum = 0;

	g_ArrayMsgIndex[3] = &g_DevNetStTable.NodeMsgInfo[3].u8Index;
	
	NODE_MSG_INFO_T *spProcNodeInfo = NULL;
	
	spProcNodeInfo = LOS_DL_LIST_ENTRY(g_ArrayMsgIndex[3], NODE_MSG_INFO_T, u8Index);
	DBG_TRACE_INFO("CurrSeqNum = %d\n", spProcNodeInfo->u16CurrSeqNum);
}


//获取当前要处理节点下标
MY_U8 GetCurrentProcNodeIndex(MSG_INFO_TABLE_T *pDevNetStTable)
{
	MY_U8 	i = 0;
	MY_U8	u8RetIndex = MAX_NODE_PROC_NUM;
	MY_U16	u16CmpSeqNum = 0;
	
	//先找出第一个有效载体的下标
	for(i=0; i<MAX_NODE_PROC_NUM; i++)
	{
		if(E_MSG_LOAD == pDevNetStTable->NodeMsgInfo[i].u8IsLoad)
		{
			u16CmpSeqNum = pDevNetStTable->NodeMsgInfo[i].u16CurrSeqNum;
			u8RetIndex = i;
			DBG_TRACE_INFO("First u8RetIndex = %d\n", u8RetIndex);
			break;
		}
	}
	
	for(i=0; i<MAX_NODE_PROC_NUM; i++)
	{
		if(E_MSG_LOAD == pDevNetStTable->NodeMsgInfo[i].u8IsLoad)
		{
			if(pDevNetStTable->NodeMsgInfo[i].u16CurrSeqNum < u16CmpSeqNum)
			{
				u16CmpSeqNum = pDevNetStTable->NodeMsgInfo[i].u16CurrSeqNum;
				u8RetIndex = i;
				DBG_TRACE_INFO("u8RetIndex = %d\n", u8RetIndex);
			}
		}
	}

	return u8RetIndex;
}


void DevNetStTableTest(void)
{
	MY_U8 u8NowProcIndex = 0;
	
	u8NowProcIndex = GetCurrentProcNodeIndex(&g_DevNetStTable);
	DBG_TRACE_INFO("u8NowProcIndex = %d\n", u8NowProcIndex);
}








